// login.js
const users = [
    { username: 'user1', password: '123456' },
    { username: 'user2', password: '123456' },
    // ...other users
];

const h1 = document.querySelector(".left_title h1");
const title = document.querySelector(".left_title");
const denglu = document.querySelector(".right_denglu");
const submit = document.querySelector(".submit_button");

denglu.addEventListener('click', function () {
    denglu.classList.add("right");
    title.classList.add("left");
    h1.classList.add('h1-change');
});

function checkUser(username, password) {
    const user = users.find(u => u.username === username && u.password === password);
    return user !== undefined;
}

submit.addEventListener('click', function (event) {
    event.preventDefault();

    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value.trim();

    if (!username || !password) {
        const errorMessage = document.getElementById('error-message');
        errorMessage.textContent = 'Username or password cannot be empty';
        return;
    }

    if (checkUser(username, password)) {
        // Use username as the storage key
        const userInfoKey = `userInfo-${username}`;
        let userInfo = JSON.parse(localStorage.getItem(userInfoKey)) || {
            username,
            avatar: '../images/Default Avatar.jpg', // Default avatar
            phone: '',
            email: '',
            gender: '',
            birthday: ''
        };

        localStorage.setItem(userInfoKey, JSON.stringify(userInfo)); // Store user information
        localStorage.setItem('currentUsername', username); // Store current username
        localStorage.setItem('isLoggedIn', 'true'); // Set login status
        window.location.href = "../test index首页/index.html";
        console.log('Login successful');
    } else {
        const errorMessage = document.getElementById('error-message');
        errorMessage.textContent = 'Incorrect username or password';
    }

    document.getElementById('password').value = '';
});